import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from "@/stores/index.js";

const router = createRouter({

  history: createWebHistory(import.meta.env.BASE_URL),

  routes: [
    { path: '/login', component: () => import('@/views/login/LoginPage.vue') },
    {
      path: '/',
      component: () => import('@/views/layout/LayoutContainer.vue'),
      redirect: '/SelectedCourse/SelectedCourseSearch',
      children: [
        {
          path: '/SelectedCourse/SelectedCourseSearch',
          component: () =>
            import('@/views/SelectedCourse/SelectedCourseSearch.vue')
        },
        {
          path: '/Course/CourseSearch',
          component: () => import('@/views/Course/CourseSearch.vue')
        },
        {
          path: '/CourseManage/CourseAdd',
          component: () => import('@/views/CourseManage/CourseAdd.vue')
        },
        {
          path: '/CourseManage/CourseDel',
          component: () => import('@/views/CourseManage/CourseDel.vue')
        },
      ]
    }
  ]
})

router.beforeEach((to) => {
  const userStore = useUserStore()
  if (!userStore.token && to.path !== '/login') return '/login'
})
export default router
